<?php
class Admin_Form_ValidateNewsCategory extends Zendvn_Validate_Form{

	
	public function __construct($arrParam = null, $options = null){
		
		define('NAME_ID',$arrParam['id']);
		$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
												'fileConfig'=>'validates/news-category.ini'));
		if($arrParam['action']=='edit'){
			$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
					'fileConfig'=>'validates/news-category.ini',
					'section'=>'edit'));
		}
		//================================Validate Name================================
		$name  = $config->getConfig('name');
		$validator  =  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['name'])){
			$message = $validator->getMessages();
			$this->_arrError['name'] = current($message);
			$arrParam['name'] = '';
		}
		//================================Validate Html_descript================================
		if($arrParam['html_descript']){
			$name  = $config->getConfig('html_descript');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['html_descript'])){
				$message = $validator->getMessages();
				$this->_arrError['html_descript'] = current($message);
				$arrParam['html_descript'] = '';
			}
		}
		//================================Validate html_keyword================================
		if($arrParam['html_keyword']){
			$name  = $config->getConfig('html_keyword');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['html_keyword'])){
				$message = $validator->getMessages();
				$this->_arrError['html_keyword'] = current($message);
				$arrParam['html_keyword'] = '';
			}
		}
		//================================Validate Ordering================================		

		if($arrParam['ordering']){
			$name = $config->getConfig('ordering');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['ordering'])){
				$message = $validator->getMessages();
				$this->_arrError['ordering'] = current($message);
				$arrParam['ordering'] = '';
			}
		}
		/*=======================================================
		 * Start - validate image
		 *=======================================================*/
		$error = Admin_Form_NewsCategory_ValidateImage::validate($arrParam, $options);
		if($error != ''){
			$this->_arrError['image'] = $error;
			$arrParam['image'] = '';
		}else{
			if($arrParam['action'] == 'edit'){
				$this->_arrData['image'] = $arrParam['current_image'];
			}
		}
		
		$this->_arrData = $arrParam;
	}
	//return true|false
	public function isVaild(){
		$flag = false;
		if(count($this->_arrError) > 0 && $this->_arrError != ''){
			$flag = true;
		}
		return $flag;
	}
	public function getData(){
		if(!$this->isVaild()){
			$arrFile = $this->upload();
			if(count($arrFile)>0){
				$this->_arrData = array_merge($arrFile,$this->_arrData);
			}
		}
		return $this->_arrData;
	}
	public function getMessageErrors(){
		return $this->_arrError;
	}
	
	public function upload(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrData['module'], 'fileConfig'=>'news-category.ini'));
		$imageConfig = $config->getConfig('category');
		/*=======================================================
		 * Start - upload picture
		 *=======================================================*/
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('image');
		$fileName = $fileInfo['image']['name'];
		$filter = new Zendvn_Filter_FileName();
		if(!empty($fileName)){		
			$options = array('rename'=> true,'prefix'=>$filter->filter($fileName));			
			$fileName = $upload->upload($imageConfig['uploadDir'],'image',$options);
			$arrFile['image'] = $fileName;
			
			$img = Zendvn_File_Image::create($imageConfig['uploadDir'] . '/' . $fileName);
			$img->resize($imageConfig['thumb-width'],$imageConfig['thumb-height'])
				->save($imageConfig['thumbDir']. '/' . $fileName);
			if($this->_arrData['action'] == 'edit'){
				$upload->remove($imageConfig['uploadDir'],$this->_arrData['current_image']);
				$upload->remove($imageConfig['thumbDir'],$this->_arrData['current_image']);
			}
		}		
		return $arrFile;
	}
}

